<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教师" prop="teacherId">
        <el-input
          v-model="queryParams.teachName"
          placeholder="请输入教师"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="courseList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" align="center" width="60"/>
      <el-table-column label="课程描述" align="center"  prop="des">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.des" placement="top">
            <p class="showOverTooltip">{{scope.row.des}}</p>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="教师" align="center" prop="teacherName" />
      <el-table-column label="教师评价" align="center" prop="avg" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="courseItem = scope.row;choseVisible = true"
            >选课</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <el-dialog
      title="提示"
      :visible.sync="choseVisible"
      width="30%"
      center>
      <span>确定选择{{courseItem==null?'‘无’':courseItem.courseName}}课程吗？</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="choseVisible = false">取 消</el-button>
    <el-button type="primary" @click="choseCourse">确 定</el-button>
  </span>
    </el-dialog>


  </div>
</template>

<script>

import {getAllBySelect, getStudentScore, selectCourse} from "@/api/courseSystem/student";
import {getInfo} from "@/api/login";

export default {
  name: "studentChoseCourse",
  data() {
    return {
      courseItem:null,
      choseVisible:false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        des: null,
        courseName: null,
        teacherId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    choseCourse(){
      if (this.courseItem!=null){
        getInfo().then(response=>{
          // console.log({"courseId":this.courseItem.courseId,"studentId":response.user.userId})
          selectCourse({"courseId":this.courseItem.courseId,"studentId":response.user.userName}).then(res=>{
            if (res.code==200){
              this.$message.success('选课成功')
            }else {
              this.$message.error('选课失败')
            }
            this.getList();
            this.choseVisible = false
          })
        })
      }else {
        this.$message.warning('未选择课程')
      }
    },
    getList() {
      this.loading = true;
      getInfo().then(res=>{
        getAllBySelect(res.user.userName,this.queryParams).then(response=>{
          if (response.code!=200){
            this.$message.error("查询出错啦")
          }else {
            this.courseList = response.data.rows
            this.total = response.data.total
            this.loading = false;
          }
        })
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        courseId: null,
        des: null,
        courseName: null,
        teacherId: null,
        teachType: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程";
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          /*if (this.form.courseId != null) {
            updateCourse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }*/
        }
      });
    },

  }
};
</script>
<style scoped>
.showOverTooltip{
  display:-webkit-box;
  text-overflow:ellipsis;
  overflow:hidden;
  /*这里是3行*/
  -webkit-line-clamp: 3;
  -webkit-box-orient:vertical;
}
</style>
